package com.Blog.www.service.Impl;


import com.Blog.www.constant.PageConstant;
import com.Blog.www.constant.TipsConstant;
import com.Blog.www.dao.Impl.LabelDaoImpl;
import com.Blog.www.dao.LabelDao;
import com.Blog.www.po.Label;
import com.Blog.www.po.User;
import com.Blog.www.service.LabelService;
import com.Blog.www.utils.DataExamine;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 用于给用户添加文章标签用的
 *
 * @author 君未洋
 */
public class LabelServiceImpl implements LabelService {
    private static LabelDao dao = new LabelDaoImpl();
    private static DataExamine data = new DataExamine();

    @Override
    public List<Label> getLabelPage(Integer id, String labelPrior) {
        //校验下数据
        if (id <= 0) {
            return null;
        }
        if (data.numberExamine(labelPrior)) {
            labelPrior = "0";
        }
        //开始执行操作
        return dao.getLabelList(id, labelPrior);
    }

    @Override
    public HttpServletRequest addLabel(User user, String labelId, String newLabel, String[] bids, String labelPrior, HttpServletRequest req) {
        if (user == null || data.numberExamine(bids)) {
            req.setAttribute(PageConstant.BLOG_G, TipsConstant.DATA_EXCEPTION);
        } else {
            if (data.numberExamine(labelId)) {
                labelId = "0";
            }
            Integer labelID = Integer.parseInt(labelId);
            if (data.numberExamine(labelPrior)) {
                labelPrior = "0";
            }
            if (newLabel != null && !"".equals(newLabel)) {
                //有要添加的新的标签
                labelID = dao.addLabel(user.getId(), labelPrior, newLabel);
            }
            if (data.numberExamine(labelId + "") || labelID == null) {
                req.setAttribute(PageConstant.BLOG_G, TipsConstant.DATA_EXCEPTION);
            } else {
                List<String> bidList = dao.getBlogLabelList(labelPrior);
                List<String> blogIds = new ArrayList<>();
                if (bidList.size() != 0) {
                    boolean flag = false;
                    for (int i = 0; i < bids.length; i++) {
                        for (int j = 0; j < bidList.size(); j++) {
                            if (bidList.get(j).equals(bids[i])) {
                                bidList.remove(j);
                                flag = true;
                                continue;
                            }
                        }
                        if (flag) {
                            flag = false;
                            continue;
                        }
                        blogIds.add(bids[i]);
                    }
                    for (String blogId : blogIds) {
                        dao.updateLabel(blogId, labelID);
                    }
                } else {
                    for (String bid : bids) {
                        dao.updateLabel(bid, labelID);
                    }
                }
                req.setAttribute(PageConstant.BLOG_G, TipsConstant.BLOG_OPERATION_B);
            }
        }
        return req;
    }
}
